#import libraries
library(psych)
library(dplyr)
library(pwr)
library(pscl)
library(car)
library(effectsize)
library(ltm)
library(RVAideMemoire)


#load data
data <- read.csv("WP Additional Mediation Study.csv")
View(data)


#recode categorical vars with values
data$gender_coded <- recode_factor(data$gender, 
                                   '1' = 'male', 
                                   '2' = 'female',
                                   '3' = 'non_binary_third_gender', 
                                   '5' = 'prefer_not_to_say')

data$inequality_coded <- recode_factor(data$inequality,
                                       '0' = 'low',
                                       '1' = 'high')

data$exp_specific_coded <- recode_factor(data$exp_specific,
                                         '1' = 'experienced',
                                         '2' = 'inexperienced')

data$exp_general_coded <- recode_factor(data$exp_general,
                                        '1' = 'experienced',
                                        '2' = 'inexperienced')

data$exp_any_coded <- recode_factor(data$exp_any,
                                    '1' = 'experienced',
                                    '2' = 'inexperienced')

data$exp_any_coded <- recode_factor(data$exp_any,
                                    '1' = 'experienced',
                                    '2' = 'inexperienced')



#exclude participants who failed to correctly answered all recap questions at their second attempt (per pre-reg)
data_sub <- data %>% filter(recap_code == 1)
View(data_sub)
#ANALYSIS IS CONDUCTED WITH THE SUB SAMPLE#


#get the sample information
##condition
inequality_count_sub <- table(data_sub$inequality_coded) #low = 112 vs. high = 101
inequality_count_sub


##gender
gender_count<-table(data$gender_coded) #male = 133 vs. female = 112 vs. 1 non-binary vs. 4 not to say
gender_count
summary(data$age)


gender_count_sub<-table(data_sub$gender_coded) #male = 114 vs. female = 94 vs. 4 not to say
gender_count_sub


##age
summary(data_sub$age) #43.55
sd(data_sub$age) #12.47


##exp
table(data_sub$exp_specific) #w/exp = 12 vs. w/o exp = 201
table(data_sub$exp_general) #w/exp = 7 vs. w/o exp = 206
table(data_sub$exp_any_coded) #w/exp = 18 vs. w/o exp = 195


#manipulation check
##calculate the manipulation check of inequality 
data_sub <- data_sub %>%
  mutate(manipulation_check = (check_1 + check_2)/2)

##reliability 
inequality_alpha_sub <- data_sub[, c('check_1', 'check_2')]
cronbach.alpha(inequality_alpha_sub) #.95
cor(inequality_alpha_sub) #.91

##check
leveneTest(manipulation_check ~ inequality_coded, data = data_sub) #p = .61
t.test(manipulation_check ~ inequality_coded, data = data_sub, 
       alternative = "two.sided", var.equal = TRUE) #t = -16.06, df = 211, p < .001
cohen.d(manipulation_check ~ inequality_coded,  data = data_sub)  #d = 2.21

aggregate(manipulation_check ~ inequality_coded, data_sub, mean) #low = 3.33 vs. high = 5.81
aggregate(manipulation_check ~ inequality_coded, data_sub, sd) #low = 1.11 vs. high = 1.14


#main effect
##willingness
###descriptive
aggregate(willingness ~ inequality_coded, data_sub, mean) #low = 3.45 vs. high = 2.90
aggregate(willingness ~ inequality_coded, data_sub, sd) #low = 1.59 vs. high = 1.41

###ANOVA
willingness_sub <- aov(willingness ~ inequality_coded, data = data_sub)
summary(willingness_sub) #F(1,211) = 6.94, p = .009

###effect size
eta_squared(willingness_sub, partial = TRUE) #eta-squared = .032, f = .182


##lending_amount
###descriptive
aggregate(lending_amount ~ inequality_coded, data_sub, mean) #low = 1057.82 vs. high = 679.22
aggregate(lending_amount ~ inequality_coded, data_sub, sd) #low = 972.88 vs. high = 749.36

###distribution
hist(data_sub$lending_amount) #not normally distributed, therefore, median-test will be performed (per pre-reg)

### Levene's test
leveneTest(lending_amount ~ inequality_coded, data = data_sub) #p = .009

###median test
mood.medtest(lending_amount ~ inequality_coded, 
             data = data_sub, exact = FALSE) #chi-square = 8.73, p = .003

###dichotomy lending amount and get descriptive
summary(data_sub$lending_amount) #median = 506
data_sub$lending_amount_coded <- ifelse(data_sub$lending_amount <= 506, 0, 1)
table(data_sub$lending_amount_coded, data_sub$inequality_coded)  #<= median: low = 45 vs. high = 62 | > median: low = 67 vs. high = 39
prop.table(table(data_sub$lending_amount_coded,
                 data_sub$inequality_coded), margin = 2) #<= median: low = 4018 vs. high = .6139 | > median: low = .5982 vs. high = .3861

###effect size for Chi-square Test
w = sqrt(8.73/(211*1))
w #w = 0.203407

###auxiliary ANOVA
###auxiliary Welch_ANOVA
lending_amount_welch <- oneway.test(lending_amount ~ inequality_coded, data = data_sub, var.equal = FALSE)
lending_amount_welch #F(1,206.09) = 10.23, p = .002
F_value <- lending_amount_welch$statistic
df1 <- lending_amount_welch$parameter[1] 
N <- nrow(data_sub)
omega_sq_welch <- (F_value - 1) / (F_value + N)
omega_sq_welch

#mediation
##trustworthiness
###calculate 
data_sub <- data_sub %>%
  mutate(trustworthiness = (trustworthy + honest)/2)

###reliability
trustworthiness_alpha_sub <- data_sub[, c('trustworthy', 'honest')]
cronbach.alpha(trustworthiness_alpha_sub) #.91
cor(trustworthiness_alpha_sub) #.84

###descriptive
aggregate(trustworthiness ~ inequality_coded, data_sub, mean) #low = 4.17 vs. high = 3.61
aggregate(trustworthiness ~ inequality_coded, data_sub, sd) #low = 1.23 vs. high = 1.13

###type 2 ANOVA
trustworthiness_sub <- aov(trustworthiness ~ inequality_coded, data = data_sub)
summary(trustworthiness_sub) #F(1,211) = 12.13, p < .001

###effect size
eta_squared(trustworthiness_sub, partial = TRUE) #eta-squared = .054, f = .240

##perceived risk
data_sub <- data_sub %>%
  mutate(risk = (risk_1 + risk_2)/2)

###reliability
risk_alpha_sub <- data_sub[, c('risk_1', 'risk_2')]
cronbach.alpha(risk_alpha_sub) #.97
cor(risk_alpha_sub) #.93

###descriptive
aggregate(risk ~ inequality_coded, data_sub, mean) #low = 4.69 vs. high = 5.34
aggregate(risk ~ inequality_coded, data_sub, sd) #low = 1.41 vs. high = 1.20

###ANOVA
risk_sub <- aov(risk ~ inequality_coded, data = data_sub)
summary(risk_sub) #F(1,211) = 12.86, p < .001

###effect size
eta_squared(risk_sub, partial = TRUE) #eta-squared = .057, f = .0.246



#Serial Mediation Analysis
##DV = willingness
process (data = data_sub, 
         y = 'willingness', 
         x = 'inequality', 
         m = c('trustworthiness','risk'),
         model = 6,
         effsize = 1,
         total = 1,
         stand = 1,
         boot = 10000,
         modelbt = 1,
         seed = 1)


##DV = median-dichotomized lending amount
process (data = data_sub, 
         y = 'lending_amount_coded', 
         x = 'inequality', 
         m = c('trustworthiness','risk'),
         model = 6,
         effsize = 1,
         total = 1,
         stand = 1,
         boot = 10000,
         modelbt = 1,
         seed = 1)


##DV = continuous lending amount
process (data = data_sub, 
         y = 'lending_amount', 
         x = 'inequality', 
         m = c('trustworthiness','risk'),
         model = 6,
         effsize = 1,
         total = 1,
         stand = 1,
         boot = 10000,
         modelbt = 1,
         seed = 1)